package com.xbongbong.paas.service.workflow.setting.impl;

import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.workflow.settings.WorkflowGetPojo;
import com.xbongbong.paas.service.workflow.setting.ConditionNodeFieldService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.WorkflowNodeUtil;
import com.xbongbong.pro.enums.workflow.WorkflowValueTypeEnum;
import com.xbongbong.workflow.pojo.nodes.WorkflowConditionPojo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/01 20:58
 */
@Service("abstractConditionNodeFieldService")
public abstract class AbstractConditionNodeFieldService implements ConditionNodeFieldService {


    @Override
    public String conditionField(WorkflowConditionPojo workflowConditionPojo, WorkflowGetPojo workflowGetPojo) throws XbbException {
        String content = "";
        Map<String, FieldAttrEntity> explainMap = WorkflowNodeUtil.getExplainMap(workflowGetPojo, workflowConditionPojo.getBusinessType(), workflowConditionPojo.getFormId());

        String attrName = explainMap.get(workflowConditionPojo.getAttr()).getAttrName();
        String symbol = ConditionEnum.getMemoBySymbo(workflowConditionPojo.getSymbol());
        if (Objects.equals(WorkflowValueTypeEnum.FIXED_VALUE.getType(), workflowConditionPojo.getValueType())) {
            List<Object> value = workflowConditionPojo.getValue();
            if (CollectionUtils.isNotEmpty(value)) {
                content = attrName + symbol + StringUtils.join(value, "、");
            }else {
                // 为空、不为空
                content = attrName + symbol;
            }
        } else {
            content = getDynamicValue(attrName, symbol, workflowGetPojo, workflowConditionPojo);
        }
        return content;
    }

    /**
     * 获取动态值
     * @param attrName
     * @param symbol
     * @param workflowGetPojo
     * @param workflowConditionPojo
     * @return
     */
    protected String getDynamicValue(String attrName, String symbol, WorkflowGetPojo workflowGetPojo, WorkflowConditionPojo workflowConditionPojo) {
        String value;
        Map<String, FieldAttrEntity> targetExplainMap = WorkflowNodeUtil.getExplainMap(workflowGetPojo, workflowConditionPojo.getTargetBusinessType(),
                workflowConditionPojo.getTargetFormId());
        String targetAttrName = targetExplainMap.get(workflowConditionPojo.getTargetAttr()).getAttrName();
        String name = "";
        if (workflowGetPojo.getWorkflowNodeMap().containsKey(workflowConditionPojo.getTargetWorkflowNodeId())) {
            name = workflowGetPojo.getWorkflowNodeMap().get(workflowConditionPojo.getTargetWorkflowNodeId());
        }
        if (Objects.equals(ConditionEnum.RANGE.getSymbol(), workflowConditionPojo.getSymbol())
                || Objects.equals(ConditionEnum.NORANGE.getSymbol(), workflowConditionPojo.getSymbol())) {
            value = String.format(attrName + symbol, name + targetAttrName);
        } else {
            value = attrName + symbol + name + targetAttrName;
        }
        return value;
    }
}
